import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../store/auth'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/HomeView.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue'),
      meta: { requiresGuest: true }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/Register.vue'),
      meta: { requiresGuest: true }
    },
    {
      path: '/profile',
      name: 'profile',
      component: () => import('../views/Profile.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/seek',
      name: 'seek',
      component: () => import('../views/Seek.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/recruit',
      name: 'recruit',
      component: () => import('../views/Recruit.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: () => import('../views/dashboard.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/jobs',
      name: 'jobs',
      component: () => import('../views/jobs.vue'),
    },
    {
      path: '/meet',
      name: 'meet',
      component: () => import('../views/Meet.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/resources',
      name: 'resources',
      component: () => import('../views/Resources.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/message',
      name: 'message',
      component: () => import('../views/message.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/admin-login',
      name: 'admin-login',
      component: () => import('../views/AdminLogin.vue'),
      meta: { requiresGuest: true }
    },
    {
      path: '/admin',
      name: 'admin',
      component: () => import('../views/AdminDashboard.vue'),
      meta: { requiresAdmin: true }
    }
  ],
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  const isLoggedIn = authStore.isLoggedIn
  const role = authStore.role
  const isAdmin = authStore.isAdmin
  
  // 检查是否需要登录权限
  if (to.meta.requiresAuth && !isLoggedIn) {
    next({ path: '/login', query: { redirect: to.fullPath } })
    return
  }
  
  // 检查管理员权限
  if (to.meta.requiresAdmin && !isAdmin) {
    ElMessage.warning('仅管理员可访问')
    next(false)
    return
  }
  
  // 角色校验：招聘者可访问 /recruit
  if (to.path === '/recruit' && role !== 'recruiter') {
    ElMessage.warning('角色不符合，仅招聘者可访问')
    next(false)
    return
  }
  // 角色校验：求职者可访问 /seek
  if (to.path === '/seek' && role !== 'job_seeker') {
    ElMessage.warning('角色不符合，仅求职者可访问')
    next(false)
    return
  }
  
  // 如果用户已登录但访问只适用于未登录用户的页面(如登录、注册)，则重定向到首页
  if (to.meta.requiresGuest && isLoggedIn) {
    next({ path: '/' })
    return
  }
  
  next()
})

export default router
